package com.hue.netty.day03.tcp;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.util.ArrayList;
import java.util.List;

/*
    TCP的粘包和拆包的演示
 */
public class Server {
    public static void main(String[] args) {
        //bossGroup只负责处理客户端的连接请求
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        //workerGroup负责处理客户端的业务请求
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            //创建服务端的启动配置对象
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(bossGroup, workerGroup)  //设置线程组
                    .channel(NioServerSocketChannel.class)    //设置服务端的通道实现
                    .option(ChannelOption.SO_BACKLOG, 128)   //设置线程队列得到的连接个数
                    .childOption(ChannelOption.SO_KEEPALIVE, true)  //设置保持活动连接状态
                    .childHandler(new ServerInitializer());
            ChannelFuture channelFuture = bootstrap.bind(7777).sync();
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
